Better come to the solution for coding exercise 30 and then to import panels as an umpire and see porn
and we import the Dow Jones Industrial Average index price data and we create a daytime index and safer
the sliding data frame and the rabbit Dow Jones so let's inspect here the Dow Jones index price data
and actually the Dow Jones index also stock market index that indicates that the value of 30 large U.S.
companies and these 30 companies are also included in the S&P 500 index and actually the constituents
are price weighted.
So we will see this in the next section but uh first of all we are only interested here in the close
price and therefore we selected the column close and uh we create a data frame instead of having a panel
series so we overwrite.
The variable down tones and then we create the simple moving average 50 year with a window of 50 and
add another column here to our data frame and we can do this by selecting the close price and use the
rolling method and we have a rolling window of 50.
And finally we are actually interested in the mean or the average of the last 50 days.
So we change the mean method so here we have added the new column simple moving average 50 and the same
we can also do for the simple moving average 200.
With a window of 200.
So here we have the close price.
The simple moving average 50 and 200.
And we can also visualize those three time series with the plot method so uh we we have the close price
in blue.
The simple moving average 50 and green and 200 in red and they're actually trading strategies based
on simple moving averages.
So the general rule is that whenever the simple moving average with the smaller window as above the
simple moving average with the larger window.
So whenever these simple moving average 50 is above these simple moving average 200 then this could
be a sign to purchase or to buy the index or to invest into the index and the rationale behind this
strategy is that the most recent price trend will persist in the near future.
And now let's assume we are at the 15th of May to 17 and we shall verify whether a trader with a simple
moving average momentum strategy was invested in the Dow Jones said to this day and therefore we uh
select uh the respective day and we can see that the average index price of value for the immediately
preceding 50 days was at twenty thousand seven hundred and the SMI.
Two hundred is nineteen thousand.
So the shorter simple moving average is above the longer simple moving average.
And this could be a sign to invest in to the Dow Jones index at this stay here.
Next we have to convert the Dow Jones so closing prices to a monthly frequency.
And of course we can do this with the free sample method and the we pass a period to the kind parameter
and we select for each and every month.
The very last trading price.
And also here this is a panel series and we transform this to a data frame and we save it here in the
variable Dow Jones monthly so here we have a monthly price data and in the investment and funds industry.
It is common practice to report past performance based on monthly returns and typically based on the
last 36 months.
So we calculate your monthly returns by using the percentage change method and to be at year another
column return so here we have monthly prices and monthly returns.
And then we can also calculate the 36 month rolling mean return.
And we can add another column here and we can simply do this by selecting the return column and change
the rolling method with a window of 36 months and calculate the mean so that's the rolling mean return
here.
And the same we can also do for the risk.
So we can calculate the 36 months a rolling standard deviation of returns.
And again we select here the returns to the column and say in the rolling method with a window of 36.
And then we calculate the standard deviation.
So here we have a rolling risk and return for the Dow Jones based on monthly return data and also here
might be more attractive to annualize the risk and return.
And we can annualized the return by multiplying the return with twelve and the same we can do with risk
and standard deviation units by multiplying it with the square root of twelve.
So this is nothing new.
And here we have for the annualized rolling risk and rolling return.
And we can also plot both.
So we selected the last two columns and use the plot method
and here we have and blue the rolling mean return and in green the rolling standard deviation of returns.
And actually here we can see that whenever the risk increases the return decreases and vice versa.
So it seems like there's a negative correlation between risk and return.
Next we shall import the column state and close for the S&P 500 Total Return Index.
And as you might recall in the video lectures we worked with the S&P 500 price return index that does
not include dividends.
So here we are working with the Total Return Index and we import the CSP file and also here we transform
the column with data information to daytime index.
And we only use the columns that date and close
so that's the total return price data.
And we can also down sample the price data with the free sample method to annual frequency and we select
the very last the trading price of each year.
So we we have annual price data starting from nineteen hundred eighty eight and our plan planets to
create the return triangle for the S&P 500 and therefore first of all we calculate lucky returns.
So in this case it's better to have a lock returns than simple returns and we can do this by dividing
each and every price by the immediately preceding price of the preceding year.
And we use here the end Peter lock method and be safe actually.
The new data frame and the variable annual returns.
And actually we rename the column label to return.
So that makes more sense.
So here we have the annual returns annual lock returns.
And actually we are starting at the return for the year nineteen hundred eighty nine.
So we have a period of 30 years and therefore offers a five week waiting list starting with a three
to one.
So we save the list here in the viable windows.
This is a list comprehension here and then we read the 30 new columns and each column stands for a rolling
return with a window from 30 years to one year.
And we can do this year with a for loop and uh for example let's take the rolling window of 10 and we
create here the column 10 years and then we take the return column and calculate the rolling mean return
for the last the immediately preceding 10 years so let's have a look and here we have uh the 30 new
columns from 30 years that the one year
and to create the triangle data frame.
We simply have to drop the returns column.
And of course we can do this with the drop method.
And that's our final triangle data frame.
And then we can pass up the triangle data frame to seaborne heat map and we use a red yellow green color
map and we determine that we want to have yellow color at a mean lock return of 0 and a deep red color
at an average lock return of minus 10 percent and a deep green color at a average return of 15 percent.
So let's have a look here.
So that's uh the return triangle for the S&P 500 Total Return Index.
And the question is here based on historical data are there any time periods of eleven years or longer
where investors lost money and let's have a look here.
So here we have investment periods of 10 years and TBA obviously a negative mean return where investors
lost money but then starting with eleven years and so on.
There's actually no historical case where investors lost the money with the S&P 500 index
and finally here we import monthly returns for the S&P 500 and the Dow Jones.
So both are here price return index
so we have two columns the S&P 500 and Dow Jones and these are monthly returns starting from nineteen
hundred ninety and before we have calculated rolling returns and rolling risk of one index.
But we can also calculate the rolling correlation between the two indexes.
So for example the S&P 500 and the Dow Jones.
So it's pretty unlikely that the correlation between those two will remain constant over time and actually
we can simply do this by selecting one index so for example the S&P 500 index then using the rolling
method with a window of thirty six and then we change the call method and TV pass that the other index.
So here the Dow Jones index.
And by doing so we are creating the rolling correlation and we save the you're all in correlation and
the variable a rolling correlation.
And let's have a look at the last the 15 timestamps.
And here we have the rolling correlation.
For the last the 15 months.
And we can also plot the rolling in correlation so it's actually no surprise that the S&P 500 and the
Dow Jones Index are highly positively correlated because actually the constituents of the cadaver Jones
index I included also in the S&P 500 index and the still the correlation is not a perfectly stable over
time.
So we have uh 0 point 9 8 but we also had 0 point 2 8 6.
And with this we are finishing with coding exercise 30 and I hope we had fun and uh looking forward
to seeing you also in the next exercise by.